package com.huwaiwai001.echo.bio.server;

import com.huwaiwai001.echo.constant.ServerInfo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by hww on 2019/3/18 11:25.
 */
public class EchoBioServerHandler implements AutoCloseable {

    private ServerSocket server;
    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    EchoBioServerHandler() throws Exception {
        this.server = new ServerSocket(ServerInfo.ECHO_PORT);
        System.out.println("服务器已启动端口号:[" + ServerInfo.ECHO_PORT + "] 开始服务 " + new Date());
    }

    void handle() throws Exception {
        boolean flag = true;
        while (flag) {
            Socket client = this.server.accept();
            EchoBioServerTask task = new EchoBioServerTask(client);
            executorService.execute(task);
        }
    }
    @Override
    public void close() throws IOException {
        server.close();
        System.out.println("服务器已关闭端口号:[" + ServerInfo.ECHO_PORT + "] 结束服务 " + new Date());
    }






}
